import java.util.HashSet;
import java.util.Scanner;

public class NGuessNumber {
    //随机生成4个0-9之间的不重复的整型数
    public Integer[] generateAnswer() {
        HashSet<Integer> set = new HashSet<>();
        while (set.size()<4){
            set.add((int)(Math.random()*9));
        }
        return set.toArray(new Integer[set.size()]);
    }
    //接收玩家输入的4个数字并返回
    public Integer [] getPlayerInput(){
        Scanner scanner = new Scanner(System.in);
        Integer[] inputInteger = new Integer[4];
        for (int i = 0; i < 4; i++) {
            inputInteger[i] = scanner.nextInt();
        }
        return inputInteger;
    }
    //比较玩家猜测和答案后返回提示信息
    public String compareGuessAnswer(Integer []int1,Integer []int2){
        int a = 0;
        //猜数字中A的个数
        for (int i = 0; i < int1.length; i++) {
            if(int1[i].equals(int2[i])){
                a++;
            }
        }
        //猜数字中b的个数
        int b = 0;
        for (Integer value : int1) {
            for (Integer integer : int2) {
                if (value.equals(integer)) {
                    b++;
                }
            }
        }
        b = b-a;
        return a+"A"+b+"B";
    }
    //根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false
    public boolean isWin(Integer []int1,Integer []int2){
        String answer = compareGuessAnswer(int1,int2);
        char a = answer.charAt(0);
        return a=='4';
    }

    public static void main(String[] args) {
        NGuessNumber nGuessNumber = new NGuessNumber();
        //生成答案
        aa:while (true){
            Integer[] answer = nGuessNumber.generateAnswer();
            //用户输入数字
            Integer[] playerInput = nGuessNumber.getPlayerInput();
            //直到猜测游戏胜利截至
            while (true){
                boolean win = nGuessNumber.isWin(answer, playerInput);
                if(!win){
                    String result = nGuessNumber.compareGuessAnswer(answer, playerInput);
                    System.out.println(result);
                    playerInput = nGuessNumber.getPlayerInput();
                }else{
                    System.out.println("游戏胜利");
                    System.out.println("是否再继续一次（y/n）?");
                    Scanner scanner = new Scanner(System.in);
                    String input = scanner.next();
                    if(input.equals("y")){
                        continue aa;
                    }else {
                        return;
                    }
                }
            }
        }
    }
}
